package com.gy.api.common.file.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gy.api.base.R;
import com.gy.api.common.file.domain.vo.FileVO;
import com.gy.api.common.file.service.IFileService;
import com.gy.util.DateUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@Transactional
@Log4j2
public class FileServiceImpl implements IFileService {
    @Value("${server.url}")
    private String server_url;
    @Value("${server.servlet.context-path}")
    private String context_path;


    @Override
    public R<FileVO> upload(MultipartFile file) {
        R r = new R();
        String fileName = file.getOriginalFilename(); // 获取文件名
        String suffixName = "";
        if (fileName.contains(".")) {
            suffixName = fileName.substring(fileName.lastIndexOf("."));// 获取文件的后缀名
        }
        String timeDirectory = DateUtils.getDate("yyyy/MMdd");
        String id = IdUtil.getSnowflakeNextIdStr();
        String newFileName = id + suffixName;//新文件名
        String relativePath = timeDirectory + "/" + newFileName;//文件上传手的相对路径
        String realPath = System.getProperty("user.dir") + "/file/" + relativePath;// 文件上传后的绝对路径
        realPath.replaceAll("//", "/");
        File dest = new File(realPath);
        if (!dest.getParentFile().exists()) {// 检测是否存在目录
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (Exception e) {
//            e.printStackTrace();
            throw new RuntimeException("数据保存失败");
        }
        FileVO vo = new FileVO();
//        vo.setId(id);
        vo.setFileName(fileName);
        vo.setFileSize(new BigDecimal((float) file.getSize() / 1024).setScale(2, BigDecimal.ROUND_HALF_UP) + "KB");
        vo.setFilePath(server_url+context_path + "file/" + relativePath);
        vo.setType(getFileType(suffixName));
        return r.setData(vo);
    }



    private Integer getFileType(String suffixName) {
        Integer type = 6;
        //文件类型：1.图片 2.视频 3.网页相关文件 4.压缩包 5.excel文件 6. 其它
        //图片 .jpg .jpeg .gif .png .bmp
        //视频 .mp4 .3gp .m4v .mpg .avi
        //网页相关文件 .html .js .css
        //压缩包 .zip .rar
        //excel文件 .xls .xlsx
        //其它
        switch (suffixName) {
            case ".zip":
                type = 4;
                break;
            case ".rar":
                type = 4;
                break;
            case ".xls":
                type = 5;
                break;
            case ".xlsx":
                type = 5;
                break;
            case ".jpg":
                type = 1;
                break;
            case ".jpeg":
                type = 1;
                break;
            case ".gif":
                type = 1;
                break;
            case ".png":
                type = 1;
                break;
            case ".bmp":
                type = 1;
                break;
            case ".mp4":
                type = 2;
                break;
            case ".3gp":
                type = 2;
                break;
            case ".m4v":
                type = 2;
                break;
            case ".mpg":
                type = 2;
                break;
            case ".mov":
                type = 2;
            case ".wmv":
                type = 2;
            case ".mkv":
                type = 2;
            case ".wav":
                type = 2;
            case ".ts":
                type = 2;
            case ".flv":
                type = 2;
            case ".avi":
                type = 2;
                break;
            case ".html":
                type = 3;
                break;
            case ".css":
                type = 3;
                break;
            case ".js":
                type = 3;
                break;
        }
        return type;
    }


}
